import request from "@/utils/request";

export default {
  getEventList(searchModel) {
    return request({
      url: "/events",
      method: "get",
      params: {
        pageNo: searchModel.pageNo,
        pageSize: searchModel.pageSize,
        organizerId: searchModel.organizerId,
        typeId: searchModel.typeId,
        status: searchModel.status,
      },
    });
  },
  getEventTypes() {
    return request({
      url: "/events/eventTypes",
      method: "get",
    });
  },
  // 获取主办方列表
  getOrganizerList() {
    return request({
      url: "/events/organizers",
      method: "get",
    });
  },
  getEventById(id) {
    return request({
      url: `/events/${id}`,
      method: "get",
    });
  },
  saveEvent(event) {
    if (event.id == null || event.id == undefined) {
      return this.addEvent(event);
    } else {
      return this.updateEvent(event);
    }
  },
  addEvent(event) {
    return request({
      url: "/events",
      method: "post",
      data: event,
    });
  },
  updateEvent(event) {
    return request({
      url: `/events`,
      method: "put",
      data: event,
    });
  },
  deleteEventById(id) {
    return request({
      url: `/events/${id}`,
      method: "delete",
    });
  },
  deleteImage(fileName) {
    if (fileName.startsWith("http://") || fileName.startsWith("https://")) {
      // 提取出文件名部分
      fileName = fileName.substring(fileName.lastIndexOf("/") + 1);
    }
    
    // 发起删除请求
    return request({
      url: `/files/${fileName}`, // 调用后端的删除接口
      method: "delete",
    });
  },
};
